// (function () {
  let totalPage = 0
  let pageNum = GetQueryString('pageNum') || 1
  // console.log(GetQueryString('pageNum'));
  fetchData()
  function fetchData() {
    axios.get('/admin/getBlogs', {
      params: {
        pageNum: pageNum,
        pageSize: 6
      }
    }).then(res => {
      // 分页
      totalPage = res.data.totalPage
      ininitialPagination()
  
      // 博客
      const blogs = res.data.data
      const tBody = document.querySelector('.blog-list tbody')
      blogs.forEach(blog => {
        const tr = document.createElement('tr')
        tr.innerHTML = `
            <th>${blog.id}</th>
            <td><img src="${imgUrl(blog.image)}" alt=""></td>
            <td class="title">${blog.title}</td>
            <td>${blog.abstract}</td>
            <td>${blog.createTime.substr(0, 10)}</td>
            <td>
              <button class="btn btn-primary edit" >编辑</button>
              <button class="btn btn-danger delete" onclick="deleteHandler(${blog.id})">删除</button>
            </td>
          `
        tBody.appendChild(tr)
      });
    })
  }
  function deleteHandler(id) {
    axios.get('/admin/deleteBlog', {
      params: { id: id }
    }).then(res => {
      console.log(res);
      if (res.data == 'success') {
        // 提示动画（1s）
       setTimeout(() => {
         location.reload()
       }, 1000);
      }
    })
  }
  
  function ininitialPagination() {
    const ul = document.querySelector('.pagination')
  
    const prev = document.createElement('li')
    prev.className = 'paginate_button page-item previous disabled'
    prev.innerHTML = `<a href="#"
      aria-controls="dataTableExample" data-dt-idx="0" tabindex="0" class="page-link">上一页</a>`
    ul.appendChild(prev)
  
    for (let i = 1; i <= totalPage; i++) {
      const li = document.createElement('li')
      console.log(pageNum == i);
      li.className = `paginate_button page-item ${pageNum == i ? 'active' : ''}`
      li.innerHTML = `<a href="blog-list.html?pageNum=${i}" aria-controls="dataTableExample" data-dt-idx="1"
        tabindex="0" class="page-link">${i}</a>`
      ul.appendChild(li)
    }
  
    const next = document.createElement('li')
    next.className = 'paginate_button page-item next'
    next.innerHTML = `<a href="#"
      aria-controls="dataTableExample" data-dt-idx="0" tabindex="0" class="page-link">下一页</a>`
    ul.appendChild(next)
  
  }